package com.nuanshui.heatedloan.entity.cms.security;

import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.nuanshui.heatedloan.entity.IdEntity;

/**
 * 部门
 */
@Entity
@Table(name = "cms_security_organization")
public class Organization extends IdEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Column(nullable = false, length = 64)
	private String name;

	@Column(length = 255)
	private String description;

	@ManyToOne
	@JoinColumn(name = "parentId")
	
	@JsonIgnore
	@JSONField(serialize=false)
	private Organization parent;

	@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "parent", fetch=FetchType.EAGER)
	@JsonIgnore
	@JSONField(serialize=false)
	private List<Organization> children = Lists.newArrayList();

	@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "organization", fetch=FetchType.EAGER)
	@JsonIgnore
	@JSONField(serialize=false)
	private Set<User> users = Sets.newHashSet();

	/**
	 * 返回 name 的值
	 * 
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * 设置 name 的值
	 * 
	 * @param name
	 */
	public void setName(final String name) {
		this.name = name;
	}

	/**
	 * 返回 description 的值
	 * 
	 * @return description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * 设置 description 的值
	 * 
	 * @param description
	 */
	public void setDescription(final String description) {
		this.description = description;
	}

	/**
	 * 返回 parent 的值
	 * 
	 * @return parent
	 */
	public Organization getParent() {
		return parent;
	}

	/**
	 * 设置 parent 的值
	 * 
	 * @param parent
	 */
	public void setParent(final Organization parent) {
		this.parent = parent;
	}

	/**
	 * 返回 children 的值
	 * 
	 * @return children
	 */
	public List<Organization> getChildren() {
		return children;
	}

	/**
	 * 设置 children 的值
	 * 
	 * @param children
	 */
	public void setChildren(final List<Organization> children) {
		this.children = children;
	}

	/**
	 * 返回 users 的值
	 * 
	 * @return users
	 */
	public Set<User> getUsers() {
		return users;
	}

	/**
	 * 设置 users 的值
	 * 
	 * @param users
	 */
	public void setUsers(final Set<User> users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return Objects.toStringHelper(this).addValue(id).addValue(name)
				.addValue(parent == null ? null : parent.getName())
				.addValue(children.size()).toString();
	}
}
